package org.qsoft.test.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import org.qsoft.test.model.Users;

/**
 * A data access object (DAO) providing persistence and search support for Users
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see org.qsoft.test.model.Users
 * @author MyEclipse Persistence Tools
 */

public class UsersDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(UsersDAO.class);
	// property constants
	public static final String SNAME = "sname";
	public static final String SPASSWORD = "spassword";
	public static final String SDEPARTMENTNAME = "sdepartmentname";
	public static final String SRANKNAME = "srankname";
	public static final String SEMAILADDR = "semailaddr";
	public static final String ISEX = "isex";
	public static final String SPOSTALADDR = "spostaladdr";
	public static final String SPOSTALCODE = "spostalcode";
	public static final String SOFFICENO = "sofficeno";
	public static final String SPHONE = "sphone";
	public static final String SFAX = "sfax";
	public static final String SPOSITION = "sposition";
	public static final String ISTATE = "istate";
	public static final String SREMARK = "sremark";
	public static final String SIPADDR = "sipaddr";

	protected void initDao() {
		// do nothing
	}

	public void save(Users transientInstance) {
		log.debug("saving Users instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public void update(Users transientInstance) {
		log.debug("saving Users instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Users persistentInstance) {
		log.debug("deleting Users instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public void deleteById(java.lang.Integer id) {
		log.debug("deleting Users instance");
		try {
			Users persistentInstance = this.findById(id);
			if(persistentInstance != null)
				getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Users findById(java.lang.Integer id) {
		log.debug("getting Users instance with id: " + id);
		try {
			Users instance = (Users) getHibernateTemplate().get(
					"org.qsoft.test.model.Users", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Users instance) {
		log.debug("finding Users instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Users instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Users as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findBySname(Object sname) {
		return findByProperty(SNAME, sname);
	}

	public List findBySpassword(Object spassword) {
		return findByProperty(SPASSWORD, spassword);
	}

	public List findBySdepartmentname(Object sdepartmentname) {
		return findByProperty(SDEPARTMENTNAME, sdepartmentname);
	}

	public List findBySrankname(Object srankname) {
		return findByProperty(SRANKNAME, srankname);
	}

	public List findBySemailaddr(Object semailaddr) {
		return findByProperty(SEMAILADDR, semailaddr);
	}

	public List findByIsex(Object isex) {
		return findByProperty(ISEX, isex);
	}

	public List findBySpostaladdr(Object spostaladdr) {
		return findByProperty(SPOSTALADDR, spostaladdr);
	}

	public List findBySpostalcode(Object spostalcode) {
		return findByProperty(SPOSTALCODE, spostalcode);
	}

	public List findBySofficeno(Object sofficeno) {
		return findByProperty(SOFFICENO, sofficeno);
	}

	public List findBySphone(Object sphone) {
		return findByProperty(SPHONE, sphone);
	}

	public List findBySfax(Object sfax) {
		return findByProperty(SFAX, sfax);
	}

	public List findBySposition(Object sposition) {
		return findByProperty(SPOSITION, sposition);
	}

	public List findByIstate(Object istate) {
		return findByProperty(ISTATE, istate);
	}

	public List findBySremark(Object sremark) {
		return findByProperty(SREMARK, sremark);
	}

	public List findBySipaddr(Object sipaddr) {
		return findByProperty(SIPADDR, sipaddr);
	}

	public List findAll() {
		log.debug("finding all Users instances");
		try {
			String queryString = "from Users";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findPage(final int i,final int limit) {
		log.debug("finding Page Users instances");
		try {
			final String queryString = "from Users";
			List list = getHibernateTemplate().executeFind(new HibernateCallback() {
				    public Object doInHibernate(Session session)
				      throws HibernateException, SQLException {
				     Query query = session.createQuery(queryString);
				     query.setFirstResult(i);
				     query.setMaxResults(limit);
				     List list = query.list();
				     return list;
				    }
				   });
				   return list;

		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Users merge(Users detachedInstance) {
		log.debug("merging Users instance");
		try {
			Users result = (Users) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Users instance) {
		log.debug("attaching dirty Users instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Users instance) {
		log.debug("attaching clean Users instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static UsersDAO getFromApplicationContext(ApplicationContext ctx) {
		return (UsersDAO) ctx.getBean("UsersDAO");
	}
}